package com.spring.boot.rabbit.configure;

import com.spring.boot.rabbit.components.DefaultConfirmCallback;
import com.spring.boot.rabbit.components.DefaultReturnCallback;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;

/**
 * rabbitmq-support项目配置类
 *
 * @author 李坤
 * @date 2022/9/1 10:44
 */
@Configuration
public class RabbitMqConfigure {

    private final Logger logger = LoggerFactory.getLogger(RabbitTemplate.class);

    @Resource
    private DefaultReturnCallback defaultReturnCallback;
    @Resource
    private DefaultConfirmCallback defaultConfirmCallback;
    @Resource
    private CachingConnectionFactory cachingConnectionFactory;

    @Bean
    public RabbitTemplate rabbitTemplate() {
        RabbitTemplate rabbitTemplate = new RabbitTemplate(cachingConnectionFactory);
        rabbitTemplate.setConfirmCallback(defaultConfirmCallback);
        rabbitTemplate.setReturnCallback(defaultReturnCallback);
        rabbitTemplate.setMandatory(true);    // 设置 交换机路由到消息队列失败时执行ReturnCallback（默认为丢弃消息）
        return rabbitTemplate;
    }

}
